package com.example.city_home_app.Fragment;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.example.city_home_app.Activity.BaseActivity;
import com.example.city_home_app.Activity.PostAddActivity;
import com.example.city_home_app.Bean.Down_Moment;
import com.example.city_home_app.Model.ApiModel;
import com.example.city_home_app.R;
import com.example.city_home_app.RecyclerViewAdapter.Adapter_Moment;
import com.example.city_home_app.RecyclerViewAdapter.Adapter_News;
import com.example.city_home_app.RetrofitListener;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

public class Fragment_moment extends BaseFragment {
    @BindView(R.id.ll_moment_new)
    LinearLayout llMomentNew;
    @BindView(R.id.ll_moment_focus)
    LinearLayout llMomentFocus;
    @BindView(R.id.ll_moment_newline)
    LinearLayout llMomentNewline;
    @BindView(R.id.ll_moment_focusline)
    LinearLayout llMomentFocusline;
    @BindView(R.id.rec_moment_content)
    RecyclerView recMomentContent;
    Unbinder unbinder;
    @BindView(R.id.refresh_moment_content)
    SwipeRefreshLayout refreshMomentContent;

    private ApiModel model = new ApiModel();
    private int current = 1;
    private Adapter_Moment adapter_moment;
    public static Fragment_moment instance;
    private RetrofitListener<List<Down_Moment>> listRetrofitListener = new RetrofitListener<List<Down_Moment>>() {
        @Override
        public void onResponse(List<Down_Moment> listener) {
            if(listener.size()==0){
                closerefrest();
            }
            adapter_moment.setList(listener);
            adapter_moment.notifyDataSetChanged();
            closerefrest();
        }

        @Override
        public void onFail(String msg) {

        }
    };

    @Override
    protected void init(View view) {
        unbinder = ButterKnife.bind(this, view);
        recMomentContent = (RecyclerView) view.findViewById(R.id.rec_moment_content);
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recMomentContent.setLayoutManager(layoutManager);
        adapter_moment = new Adapter_Moment(getActivity(),getFragmentManager());
        recMomentContent.setAdapter(adapter_moment);
        recMomentContent.setItemAnimator(new DefaultItemAnimator());

        refreshMomentContent = (SwipeRefreshLayout) view.findViewById(R.id.refresh_moment_content);
        refreshMomentContent.setColorSchemeColors(0xff49afcd);
        refreshMomentContent.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                refresh();
            }
        });

        instance = this;
    }

    @Override
    protected void initData(Bundle savedInstanceState) {

    }

    @Override
    protected int getContentLayoutId() {
        return R.layout.fragment_moment;
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @OnClick({R.id.ll_moment_new, R.id.ll_moment_focus})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ll_moment_new:
                llMomentFocusline.setBackgroundColor(0xff888888);
                llMomentNewline.setBackgroundColor(0xff42A5F5);
                setselect(1);
                break;
            case R.id.ll_moment_focus:
                llMomentFocusline.setBackgroundColor(0xff42A5F5);
                llMomentNewline.setBackgroundColor(0xff888888);
                setselect(2);
                break;
        }
    }

    private void setselect(int type) {
        current = type;
        refreshMomentContent.setRefreshing(true);
        refresh();
    }

    private void refresh() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (current == 1) {
                            model.getmomentlist(BaseActivity.getUserinfo().getCityid(), listRetrofitListener);
                        } else {
                            model.getmomentlistforfocus(BaseActivity.getUserinfo().getUserid(), BaseActivity.getUserinfo().getCityid(), listRetrofitListener);
                        }
                    }
                });
            }
        }).start();
    }

    public void closerefrest(){
        refreshMomentContent.setRefreshing(false);
    }

    @Override
    public void onResume() {
        super.onResume();
        setselect(current);
    }
}
